from os import name
from typing import List
from urllib import request
from app.exceptions.http_resp import HttpResp
from app.exceptions.exception import AppException
from fastapi import APIRouter, Depends, Header, Request
from app.api.admin.schema.system.user import UserInSchema, UserLoginSchema, UserOutSchema, UserSearchSchema
from app.api.admin.service.system.user import UserService
from app.utils.res import unified_resp

user = APIRouter(prefix="/user", tags=["用户中心"])


@user.post("/list",  response_model=List[UserOutSchema])
async def list(user_search_data: UserSearchSchema, request: Request, user_service: UserService = Depends(UserService.instance)):
    '''获取所有'''
    print("endpoit 取出 user id ")

    return await user_service.get_list(user_search_data)


@user.post("/create")
@unified_resp
async def create(user_in: UserInSchema, request: Request, user_service: UserService = Depends(UserService.instance)):
    '''新增'''
    if hasattr(request.state, "user_id"):
        user_id = request.state.user_id
    return await user_service.create(user_in)


@user.post("/login")
@unified_resp
async def login(user_login_in: UserLoginSchema, request: Request, user_service: UserService = Depends(UserService.instance)):
    # print("user_id", request.state.user_id)
    if hasattr(request.state, "user_id"):
        user_id = request.state.user_id
    return await user_service.login(user_login_in)
